#include<iostream>
#include<string.h>
using namespace std;

class TheSwap
{
public:
	int findMax(int n, int k)
	{
		int numbers[7];
		int i=0,j;
		while(n>0)
		{
			numbers[i] = n%10;
			n /= 10;
			i++;
		}
		//numbers[i] = "\0";
		int len = strlen(numbers);
		if(k > len)
		{
			return -1;
		}

		for(i=len-1;i>0,k>0;i--)
		{
			int index=0;
			for(j=0;j<i;j++)
			{
				if(numbers[j] > numbers[i] && numbers[j] > numbers[index])
				{
					index =j;
				}
			}
			if(numbers[index] > numbers[i])
			{
				int temp = numbers[index];
				numbers[index] = numbers[i];
				numbers[i] = temp;
				k--;
			}
		}
		int count=0;
		i=len-1;
		while(i>-1)
		{
			count = count*10 + numbers[i];
		}
		return count;
	}
};

#include<stdio.h>
int d10[] = {1,10,100,1000,10000,10000,100000,1000000,10000000};
bool num[1000001][10] = {false};
class TheSwap
{
	int M, count;

	int swap(int n, int i, int j)
	{
		int a = (n/d10[i]) %10;
		int b = (n/d10[j]) %10;
		return n+ d10[i]*(b-a) + d10[j]*(a-b);
	}

	void find(int n, int k)
	{
		if(num[n][k])
			return;
		num[n][k] =true;
		if(k==0)
		{
			count=count>n?count:n;
		}
		else
		{
			for(int i=0;i<M-1;i++)
				for(int j=i+1;j<M;j++)
					find(swap(n,i,j), k-1);
		}
	}
	public:
		int findMax(int n, int k)
		{
			M =0;
			for(int i=n;t>0;t/=10, M++);
			if(M == 1 || (M==2 && n%10==0))
				return -1;

			count =-1;
			find(n,k);
			return count;
		}
};